<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<meta charset="utf-8">
<head th:include="include :: header">
	<style type="text/css">
		label{
			font-size: 12px;
		}
	</style>
</head>
<body class="white-bg" >
	<div class="wrapper wrapper-content animated fadeInRight ibox-content" >
			<form class="form-horizontal m" id="form-enter-add">
				<div class="row">
					<div class="col-sm-6">
						<div class="form-group">
							<label class="col-sm-4 control-label"><span style="color: red; ">*</span>订单编号：</label>
							<div class="col-sm-8">
								<input class="form-control" placeholder="请输入订单编号" type="text" name="formNum" id="formNum" required>
							</div>
						</div>
					</div>
					<div class="col-sm-6">
						<div class="form-group">
							<label class="col-sm-4 control-label"><span style="color: red; ">*</span>旺旺号：</label>
							<div class="col-sm-8">
								<input class="form-control" placeholder="请输入旺旺号" type="text" name="wwNum" id="wwNum" required onblur="isHaveLastNun()">
								<input class="form-control"  type="hidden" name="customerColor" id="customerColor" value="0" >

							</div>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-sm-6">
						<div class="form-group">
							<label class="col-sm-4 control-label"><span style="color: red; ">*</span>请选择平台：</label>
							<div class="col-sm-8">
								<select class="form-control m-b" placeholder="请选择平台" id="enterDeptId" name="enterDeptId" required>
									<option value="">请选择平台</option>
								</select>
							</div>
						</div>
					</div>
					<div class="col-sm-6">
						<div class="form-group">
							<label class="col-sm-4 control-label"><span style="color: red; ">*</span>请选择店铺：</label>
							<div class="col-sm-8">
								<select class="form-control m-b" placeholder="请选择店铺" id="enterShopId" name="enterShopId" required>
									<option value="">请选择店铺</option>
								</select>
							</div>
						</div>
					</div>
					<div class="col-sm-6">
						<div class="form-group">
							<label class="col-sm-4 control-label"><span style="color: red; ">*</span>产品名称：</label>
							<div class="col-sm-8">
								<select class="form-control m-b" placeholder="请选择产品" id="enterProductId" name="enterProductId" required>
									<option value="">请选择产品</option>
								</select>
							</div>
						</div>
					</div>

				</div>
				<div class="row">
					<div class="col-sm-6">
						<div class="form-group">
							<label class="col-sm-4 control-label"><span style="color: red; ">*</span>佣金：</label>
							<div class="col-sm-8">
								<input class="form-control" placeholder="请输入佣金" type="number" name="commission" id="commission" required>
							</div>
						</div>
					</div>
					<div class="col-sm-6">
						<div class="form-group">
							<label class="col-sm-4 control-label"><span style="color: red; ">*</span>微信号：</label>
							<div class="col-sm-8">
								<input class="form-control" placeholder="请输入微信号" type="text" name="webChatNum" id="webChatNum" onblur="isoldOrnew()" required >
								<!--<input class="form-control" placeholder="请输入微信号" type="text" name="webChatNum" id="webChatNum" required >-->

							</div>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-sm-6">
						<div class="form-group">
							<label class="col-sm-4 control-label">好评：</label>
							<div class="col-sm-8">
								<input class="form-control" placeholder="请输入好评" type="text" name="goodRecord" id="goodRecord" >
							</div>
						</div>
					</div>
					<div class="col-sm-6">
						<div class="form-group">
							<label class="col-sm-4 control-label"><span style="color: red; ">*</span>订单性质：</label>
							<div class="col-sm-8">
								<!--<label class=""> <input type="radio" name="menuType" value="M" /> 目录 </label>-->
								<label class="radio-inline"> <input type="radio" name="formNature" value="1" />新客户</label>
								<label class="radio-inline"> <input type="radio" name="formNature" value="2" />老客户</label>
							</div>
						</div>
					</div>

				</div>
				<div class="row">
					<div class="col-sm-6">
						<div class="form-group">
							<label class="col-sm-4 control-label"><span style="color: red; ">*</span>订单金额：</label>
							<div class="col-sm-8">
								<input class="form-control" placeholder="请输入订单金额" type="number" name="orderPrice" id="orderPrice" required>
							</div>
						</div>
					</div>
					<div class="col-sm-6">
						<div class="form-group">
							<label class="col-sm-4 control-label">好评单价：</label>
							<div class="col-sm-8">
								<input class="form-control" placeholder="请输入好评单价" type="number" name="goodPrice" id="goodPrice">

							</div>
						</div>
					</div>
				</div>

				<div class="row">
					<div class="col-sm-6">
						<div class="form-group">
							<label class="col-sm-4 control-label"><span style="color: red; ">*</span>订单时间：</label>
							<div class="col-sm-8">
								<!--<input class="form-control" data-datetime="yyyy-mm-dd hh:mm:ss"  type="text" name="createDate" id="createDate" required>-->
								<input style="height: 30px" data-type="datetime" data-format="yyyy-MM-dd HH:mm:ss" type="text" class="time-input" id="createDate" name="createDate" onload="getlocalTime()" />
							</div>
						</div>
					</div>

				</div>

				<div class="row">
					<div class="col-sm-12">
						<div class="form-group">
							<label class="col-xs-2 control-label">备注：</label>
							<div class="col-xs-10">
								<textarea id="remark" name="remark" autocomplete="off" maxlength="500" class="form-control" rows="3"></textarea>
							</div>
						</div>
					</div>
				</div>
				<hr>
			</form>
		<div style="width: 100%;height: 120px;border: 1px solid grey;font-size: 14px;line-height:15px;overflow: auto;margin-top: 8px;" id = "hideNum">
		</div>

	</div>
	<div th:include="include::footer"></div>
	<script th:src="@{/ajax/libs/select/select2.js}"></script>
	<script type="text/javascript">
		var prefix = ctx + "enter/enter";


		
		$(function () {
            getAllDept()
            // getAllProduct()
            getlocalTime()

        })

		// 获取所有的平台
		function getAllDept() {
            $.ajax({
                type: "GET",
                url: prefix +'/allDeptT',
                async: false,
                contentType : 'application/json;charset=utf-8',
                traditional:true,
                success : function(data) {
                    for(var i=0;i<data.length;i++){
                        var html = "<option value='"+data[i].deptId+"'>"+data[i].deptName + "</opinion>"
                        $("#enterDeptId").append(html);
                    }
                },
                error:function(e) {
                    console.info(e);
                }
            });
        }
        $("#enterDeptId").on('change',function(){
            var deptId = $(this).val();
            $.ajax({
                type: "GET",
                url: prefix +'/allShop',
                async: false,
                data:{"deptId":deptId},
                contentType : 'application/json;charset=utf-8',
                traditional:true,
                success : function(data) {

                    $("#enterShopId").empty().append("<option value=''>请选择店铺</opinion>");
                    for(var i=0;i<data.length;i++){
                        var html = "<option value='"+data[i].id+"'>"+data[i].shopName + "</opinion>"
                        $("#enterShopId").append(html);
                    }
                },
                error:function(e) {
                    console.info(e);
                }
            });
		})

        $("#enterShopId").on('change',function(){
            var shopId = $(this).val();
            var deptId = $("#enterDeptId").val();
            //查询城市
            $.ajax({
                type: "GET",
                url: prefix +'/product',
                async: false,
                data:{"deptId":deptId,"shopId":shopId},
                contentType : 'application/json;charset=utf-8',
                traditional:true,
                success : function(data) {
                    console.log(data)
                    $("#enterProductId").empty();
                    for(var i=0;i<data.length;i++){
                        var html = "<option value='"+data[i].id+"'>"+data[i].productName + "</opinion>"
                        $("#enterProductId").append(html);
                    }
                },
                error:function(e) {
                    console.info(e);
                }
            });
        });

		//
		function isHaveLastNun(){
            history()

		}

		function getColor(){
            var ww=$.trim($("#wwNum").val());
            $.ajax({
                type: "GET",
                url: ctx +'data/data/countNum?wwNum='+ww,
                async: false,
                contentType : 'application/json;charset=utf-8',
                traditional:true,
                success : function(data) {
                    $("#hideNum").empty()
                    console.log(data)
                    var html
                    if (data.code == 0){
                        if (Number(data.msg) > 0 ){
                            $("#customerColor").val("2")
                        }else{
                            $("#customerColor").val("1")
                        }
                    }

                },
                error:function(e) {
                    console.info(e);
                }
            });
		}
		// 在输入事件之后,根据旺旺号查询是否存在导入的数据
		// function isHaveLastNun(){
		//     // 获取输入的旺旺号
         //    var wc=$.trim($("#webChatNum").val());
         //    var ww=$.trim($("#wwNum").val());
         //    $.ajax({
         //        type: "GET",
         //        url: ctx +'wwebchat/wwebchat/selectWwOrWebchat',
		// 		data:{
         //            "wwNum":ww,
         //            "webChat":wc
		// 		},
         //        async: false,
         //        contentType : 'application/json;charset=utf-8',
         //        traditional:true,
         //        success : function(data) {
         //            $("#hideNum").empty()
		// 			console.log(data)
         //            var html
         //
         //            if (data.length == 0){
         //                $("input[type=radio]").removeAttrs("disabled");
         //                $("input:radio[value=1]").prop('checked',true);
         //                // html  = "不存在历史数据！"
         //                // $("#hideNum").append(html)+"<br/>"+"<hr>"
         //            }else {
         //
         //                $("input:radio[value=2]").prop('checked',true);
         //                $("input[type=radio]").prop('disabled',true);
        //
         //            }
         //            history();
         //        },
         //        error:function(e) {
         //            console.info(e);
         //        }
         //    });
        //
		// }
		// function isoldOrnew(){
		// 	var wc=$.trim($("#webChatNum").val());
		// 	var ww=$.trim($("#wwNum").val());
		// 	// $("input[type='radio']").removeAttr('checked');
		// 	if(wc!=""){
		// 		$.ajax({
		// 			type: "GET",
		// 			url: ctx +'wwebchat/wwebchat/selectWwOrWebchat',
		// 			data:{
		// 				"wwNum":ww,
		// 				"webChat":wc
		// 			},
		// 			async: false,
		// 			contentType : 'application/json;charset=utf-8',
		// 			traditional:true,
		// 			success : function(data) {
		// 				$("#hideNum").empty()
		//
		// 				var html
		// 				if (data.length == 0){
		// 					$("input[type=radio]").removeAttrs("disabled");
		// 					$("input:radio[value=1]").prop('checked',true);
		//
		// 				}else {
		//
		// 					$("input:radio[value=2]").prop('checked',true);
		// 					$("input[type=radio]").prop('disabled',true);
		//
		// 				}
		//
		// 				history();
		// 			},
		// 			error:function(e) {
		// 				console.info(e);
		// 			}
		// 		});
		// 	}
		//
		// }
        function isoldOrnew(){
		    history()
		}
function history(){
    var wc=$.trim($("#webChatNum").val());
    var ww=$.trim($("#wwNum").val());
    $.ajax({
        type: "GET",
        url: prefix +'/selectByWWechat',
        data:{
            "wwNum":ww,
            "webChatNum":wc
        },
        async: false,
        contentType : 'application/json;charset=utf-8',
        traditional:true,
        success : function(data) {
            console.log(data)
            $("#hideNum").empty()
            var html
            if (data.length == 0){
                					$("input[type=radio]").removeAttrs("disabled");
                					$("input:radio[value=1]").prop('checked',true);
                					getColor()
            }else {
                					$("input:radio[value=2]").prop('checked',true);
                					$("input[type=radio]").prop('disabled',true);
                for(var i=0;i<data.length;i++){
                    // $("input[name='formNature'][value=1]").attr("checked",true)
                    html = "订单号："+data[i].formNum+"；"+"平台："+data[i].deptName+"；"+"产品："+data[i].productName+"；"+"录单员："+data[i].userName+"；"+"旺旺号："+data[i].wwNum+"；"+"微信号："+data[i].webChatNum+"；"+"佣金："+data[i].commission+"；"+"录入时间："+data[i].createDate+"<br/>"
                    $("#hideNum").append(html)
                }

            }
        },
        error:function(e) {
            console.info(e);
        }
    })
}

        $("#form-enter-add").validate({
            onkeyup: false,
            rules:{
                formNature:{
                    required:true,
                },
                formNum:{

                    formNum:false,
                    remote: {
                        url: prefix + "/checkFormNum",
                        type: "post",
                        dataType: "json",
                        data: {
                            "formNum" : function() {
                                return $.common.trim($("#formNum").val());
                            }
                        },
                        dataFilter: function(data, type) {
                            return $.validate.unique(data);
                        }
                    }
                },
            },
            messages: {
                "formNum": {
                    remote: "该订单编号已经存在"
                },
            },
            focusCleanup: true
        });

function getlocalTime(){
    $("#createDate").val(getFormatDate())
}

        function getFormatDate(){
            var nowDate = new Date();
            var year = nowDate.getFullYear();
            var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
            var date = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();
            var hour = nowDate.getHours()< 10 ? "0" + nowDate.getHours() : nowDate.getHours();
            var minute = nowDate.getMinutes()< 10 ? "0" + nowDate.getMinutes() : nowDate.getMinutes();
            var second = nowDate.getSeconds()< 10 ? "0" + nowDate.getSeconds() : nowDate.getSeconds();
            return year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second;
        }
		function submitHandler() {
		    $("input[type=radio]").removeAttrs("disabled");

	        if ($.validate.form()) {
                console.log($('#form-enter-add').serialize())
	        	$.operate.save(prefix + "/saveEnterForm", $('#form-enter-add').serialize());
	        }
	    }


	    
	</script>
</body>
</html>
